package com.xiaodong.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaodong.reggie.currency.R;
import com.xiaodong.reggie.dto.DishDto;
import com.xiaodong.reggie.dto.SetmealDto;
import com.xiaodong.reggie.entity.Category;
import com.xiaodong.reggie.entity.Dish;
import com.xiaodong.reggie.entity.Setmeal;
import com.xiaodong.reggie.entity.SetmealDish;
import com.xiaodong.reggie.service.CategoryService;
import com.xiaodong.reggie.service.DishService;
import com.xiaodong.reggie.service.SetmealDishService;
import com.xiaodong.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    //查询分类数据
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {

        Page<Setmeal> pageInfo = new Page(page, pageSize);

        Page<SetmealDto> pageInfo1 = new Page();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.orderByAsc(Setmeal::getUpdateTime);

        if (name != null) {
            queryWrapper.like(Setmeal::getName, name);
        }

        setmealService.page(pageInfo, queryWrapper);

        //拷贝对象
        BeanUtils.copyProperties(pageInfo, pageInfo1, "records");

        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {

            SetmealDto setmealDto = new SetmealDto();
            //拷贝对象
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            String name1 = byId.getName();
            setmealDto.setCategoryName(name1);
            return setmealDto;

        }).collect(Collectors.toList());

        pageInfo1.setRecords(list);

        return R.success(pageInfo1);
    }

    //新增套餐和套餐中菜品
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)  //删除全部套餐数据
    public R<String> save(@RequestBody SetmealDto setmealDto) {

        setmealService.saveByDto(setmealDto);

        return R.success("添加了一道新菜");

    }

    //数据回显
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id) {

        SetmealDto dishAndFlavor = setmealService.getDishAndFlavor(id);

        if (dishAndFlavor != null) {
            return R.success(dishAndFlavor);
        }

        return R.error("没有查询到对应菜品信息");
    }

    //修改套餐类型
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {

        setmealService.updateDishAndFlavor(setmealDto);
        return R.success("修改成功");

    }

    //停售
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable int status, Long[] ids) {

        for (int i = 0; i < ids.length; i++) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(ids[i]);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("该菜品已经修改成功");

    }

    //删除菜品
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)  //删除全部套餐数据
    public R<String> delete(Long[] ids) {


        List<Long> list = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            list.add(ids[i]);
        }

        setmealService.removeByIds(list);

        return R.success("删除成功");

    }

    //查询套餐和菜品
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<SetmealDto>> listByCategoryIdAndStatus(Setmeal setmeal) {

        //根据categoryId和status查询套餐数据
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(Setmeal::getStatus, setmeal.getStatus());
        List<Setmeal> lists = setmealService.list(queryWrapper);

        if (lists != null) {
            //根据套餐id查询套餐中的菜品

            List<SetmealDto> list = lists.stream().map((item) -> {
                SetmealDto setmealDto = new SetmealDto();
                BeanUtils.copyProperties(item, setmealDto);

                LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(SetmealDish::getSetmealId, item.getId());
                List<SetmealDish> list1 = setmealDishService.list(queryWrapper1);

                setmealDto.setSetmealDishes(list1);

                return setmealDto;
            }).collect(Collectors.toList());

            if (list != null) {
                return R.success(list);
            }
        } else {
            return R.error("没发现该套餐");
        }
        return R.error("该套餐没有绑定菜品");

    }

    //根据套餐id查询套餐中的
    @GetMapping("/dish/{id}")
    public R<List<SetmealDish>> dish(@PathVariable Long id){

        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        if(list!=null){
            return R.success(list);
        }

        return R.error("没有菜品");

    }
}
